package com.org.shichui.service;

import com.org.shichui.dao.UserMapper;
import com.org.shichui.model.BaseResponse;
import com.org.shichui.pojo.User;
import com.org.shichui.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 登录
     * @param phone
     * @param password
     * @return
     */
    public User login (String phone,String password){
        User login = userMapper.login(phone, password);
        return login;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    public BaseResponse register(User user) {
        if(StringUtil.isBlank(user.getPassword())){
            return new BaseResponse(100,"密码不能为空",null);
        }
        if(StringUtil.isBlank(user.getPhone())){
            return new BaseResponse(100,"手机号不能为空",null);
        }
        if(StringUtil.isBlank(user.getQq())){
            return new BaseResponse(100,"QQ号不能为空",null);
        }
        User checkUser = userMapper.selectByPhone(user.getPhone());
        if(checkUser!=null){
            return new BaseResponse(100,"账号重复",null);
        }
        user.setId(StringUtil.getUUID());
        user.setCreatTime(new Date());
        //注册的用户是不能登陆的
        user.setStatus(0);
        int insert = userMapper.insert(user);
        if(insert==1){
            return new BaseResponse(200,"注册成功!",null);
        }
        return new BaseResponse(100,"注册失败",null);
    }
}
